<template>
	<uni-popup ref="popup" type="bottom" :maskClick="true">
		<view class="content">
			<view class="hd">
				<text class="title">给客户打招呼</text>
				<icon class="iconfont icon" @click="close()">&#xe733;</icon>
			</view>
			<form class="form" @submit="sub">
				<view class="textarea">
					<textarea v-model="data.txt" name="callTxt" placeholder="输入打招呼的内容" maxlength="120" />
					<view class="num"><text>{{data.txt.length}}/{{data.maxNum}}</text></view>
				</view>
				<view class="btns">
					<button class="btn" form-type="submit">提交</button>
					<label class="radio" @click="data.checked=!data.checked">
						<radio-group name="directsend">
							<radio value="r1" :checked="data.checked" color="#E1835B" style="transform:scale(0.6)" />
						</radio-group>
						<view class="txt"><text>下次直接发送</text></view>
					</label>
				</view>
			</form>
		</view>
	</uni-popup>
</template>
<script setup>
	import { ref, reactive, watch, computed } from 'vue';
	import { call_hall, call_my } from '@/subpages/common/api/merchant/call.js';
	import { valid } from '@/subpages/common/utils/valid.js';
	import { showModal } from '@/common/hooks/useShow.js';
	const emits = defineEmits(['changeBtnStatus']);
	const data_props = defineProps({
		autoSend: { // 是否下次直接发送
			type: [String, Number],
			default: 1
		},
		type: {
			type: String,
			default: 'my'
		}
	});
	const popup = ref(null);
	const data = reactive({
		checked: false,
		maxNum: 120,
		txt: '',
		item: {}
	});
	watch(() => data_props.autoSend, (newVal) => {
		data.checked = newVal ? true : false;
	}, { immediate: true })

	function open(item) {
		data.txt = '';
		data.item = item;
		popup.value.open();
	}

	function close() {
		popup.value.close();
	}

	function sub(e) {
		let subData = e.detail.value;
		let rules = [{
			value: subData.callTxt,
			null: '请输入打招呼的内容~',
			dataType: '10&120',
			error: '请输入10-120个字符~'
		}];
		const dataTypes = {
			dataTypes: { '10&120': /^[\w\W]{10,120}$/ },
			tip: 1
		};
		valid(rules, dataTypes).then(res => {
			if (data_props.type == 'my') callMy();
			else callHall();
		})
	}
	// 客户大厅打招呼
	async function callHall(item) {
		let code = 0,
			resData = null;
		if (item) data.item = item;
		try {
			resData = await call_hall({
				greetings: data.checked && !data.txt ? '' : data.txt,
				defaultgreetings: data.checked ? 1 : 0,
				uid: data.item.mid
			});
			code = resData.code;
		} catch (e) {
			resData = e;
			code = e.code;
		}
		switch (code) {
			case 200:
				uni.showToast({
					icon: 'none',
					mask: true,
					title: `已向客户发送打招呼消息~`
				})
				emits('changeBtnStatus', { item: data.item, uid: resData.data.uid });
				break;
			case 201:
				showModal({
					content: `“客户大厅”今日打招呼次数已用完，上传文档可每日额外获得10次`,
					showCancel: false
				})
				emits('changeBtnStatus', { item: data.item, uid: resData.data.uid });
				break;
			case 202:
				uni.showToast({
					icon: 'none',
					mask: true,
					title: `已向客户发送打招呼消息，“客户大厅”今日剩余打招呼次数：${resData.data.count}`
				})
				emits('changeBtnStatus', { item: data.item, uid: resData.data.uid });
				break;
			case -201:
				showModal({
					content: `管理员已禁用您的操作功能，原因是：${resData.data.msg}。若屡次发现违规，平台将禁止您使用本系统！如有疑问可咨询QQ：${resData.data.qq}`,
					showCancel: false
				})
				break;
			case -202:
				showModal({
					content: `“客户大厅”今日打招呼次数已用完，去“我的客户”继续交流`,
					showCancel: false
				})
				break;
			case -203:
				showModal({
					content: `该客户暂不在线，无法打招呼！ 温馨提示：请在客户在线时尽快打招呼，以免错失更多获客机会`,
					showCancel: false
				})
				break;
			case -204:
				uni.showToast({
					icon: 'none',
					mask: true,
					title: `该用户暂不能联系，试试给其他客户打招呼吧~`
				})
				break;
			case -300:
				// this.$parent.forbidLayer(resData);
				showModal({
					title: '功能已被禁用',
					content: `${resData.data.msg}`,
					showCancel: false
				})
				break;
			default:
				uni.showToast({
					icon: 'none',
					mask: true,
					title: resData.message
				})
				return;
		}
		close();
	}
	// 我的客户打招呼
	async function callMy(item) {
		let code = 0,
			resData = null;
		if (item) data.item = item;
		try {
			resData = await call_my({
				greetings: data.checked && !data.txt ? '' : data.txt,
				defaultgreetings: data.checked ? 1 : 0,
				uid: data.item.mid
			});
			code = resData.code;
		} catch (e) {
			resData = e;
			code = e.code;
		}
		switch (code) {
			case 200:
				uni.showToast({
					icon: 'none',
					mask: true,
					title: `已向客户发送打招呼消息~`
				})
				emits('changeBtnStatus', { item: data.item, uid: resData.data.uid });
				break;
			case -201:
				showModal({
					content: `该客户暂不在线，无法打招呼！ 温馨提示：请在客户在线时尽快打招呼，以免错失更多获客机会`,
					showCancel: false
				})
				break;
			case -202:
				showModal({
					content: `该用户暂不能联系，试试给其他客户打招呼吧~`,
					showCancel: false
				})
				break;
			case -203:
				showModal({
					content: `管理员已禁用您的操作功能，原因是：${resData.data.msg}。若屡次发现违规，平台将禁止您使用本系统！如有疑问可咨询QQ：${resData.data.qq}`,
					showCancel: false
				})
				break;
			case -300:
				// this.$parent.forbidLayer(resData);
				showModal({
					title: '功能已被禁用',
					content: `${resData.data.msg}`,
					showCancel: false
				})
				break;
			case 10001:
				uni.showToast({
					icon: 'none',
					mask: true,
					title: `${resData.message}`
				})
				break;
		}
		close();
	}
	defineExpose({
		open,
		close,
		callHall,
		callMy
	})
</script>
<style lang="scss" scoped>
	@import '@/common/css/base.scss';
	.content {
		width: 100%;
		border-radius: 16rpx 16rpx 0 0;
		background-color: #fff;
	}
	.hd {
		@include flex;
		height: 110rpx;
		padding: 0 28rpx;
		border-bottom: 1px solid #ddd;
		.title {
			font-size: 36rpx;
			color: #000;
		}
		.icon {
			font-size: 36rpx;
			color: #666;
		}
	}
	.form {
		display: block;
		padding: 40rpx 28rpx;
		box-sizing: border-box;
	}
	.textarea {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 300rpx;
		border: 2rpx solid #DDDDDD;
		border-radius: 8rpx;
		textarea {
			flex: 1;
			width: 100%;
			padding: 24rpx 24rpx 0;
			box-sizing: border-box;
			font-size: 30rpx;
			color: #000;
		}
		.num {
			height: 60rpx;
			line-height: 60rpx;
			padding: 0 20rpx;
			text-align: right;
			font-size: 22rpx;
			color: #aaa;
		}
	}
	.btns {
		margin: 40rpx 0 0;
		.btn {
			width: 100%;
			height: 88rpx;
			line-height: 88rpx;
			border: 0;
			background: $color;
			border-radius: 10rpx;
			font-size: 36rpx;
			color: #fff;
			&::after {
				border: 0;
			}
		}
		.radio {
			@include flex(row, flex-start);
			margin-top: 20rpx;
			.txt {
				margin-left: -10rpx;
				font-size: 24rpx;
				color: #999;
			}
		}
	}
</style>
